package com.bluemor.reddotface.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bluemor.reddotface.R;
import com.bluemor.reddotface.activity.ScrollViewX.OnScrollListener;
import com.bluemor.reddotface.adapter.HomeListviewAdapter;
import com.bluemor.reddotface.adapter.ImageAdapter;
import com.bluemor.reddotface.adapter.ItemAdapter;
import com.bluemor.reddotface.adapter.ItemListAdapter;
import com.bluemor.reddotface.util.Util;
import com.bluemor.reddotface.view.DragLayout;
import com.bluemor.reddotface.view.DragLayout.DragListener;
import com.bluemor.reddotface.view.MyRelativeLayout;

public class MainActivity extends Activity implements OnTouchListener,
		OnItemClickListener {

	
	private DragLayout dl;
	private ImageAdapter adapter;
	private LinearLayout lv;
	private ScrollViewX mainScroll;
	private ArrayList<View> itemViewList = null;// 存储item视图
	private ItemAdapter itemAdapter = null;
	// private TextView tv_noimg;
	private ImageView iv_bottom;
	private RadioButton iv_icon;
	int width = 0;
	int height = 0;
	int currentPosition = 0;
	float downY = 0f;
	private double[] positions = new double[3];
	private boolean scrollFlag = true;
	private HomeListviewAdapter hAdapter;
	private int[] images = {R.drawable.tu0,R.drawable.tu1,R.drawable.tu2,R.drawable.tu3};
	private List<Bitmap> image = new ArrayList<Bitmap>();
	private ListView listView_home;
	private TextView tv_mail;
	private MyRelativeLayout mainContentRl;
	private ItemListAdapter itemListAdapter1;
	private ItemListAdapter itemListAdapter2;
	private ItemListAdapter itemListAdapter3;
	private List<Map<String, String>> itemList =null;
			private List<Map<String, String>> itemList2 =null;
					private	List<Map<String, String>> itemList3 =null;
	private int positionClick=0;
	// private List<String> itemList=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		DisplayMetrics displayMetrics = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		width = displayMetrics.widthPixels;
		height = displayMetrics.heightPixels;
		positions[1] = height * 0.7 * 0;
		positions[2] = height * 0.7 * 1;
		positions[0] = height * 0.7 * 2;
		Util.initImageLoader(this);
		initDragLayout();
		initView();
		mainScroll = (ScrollViewX) super.findViewById(R.id.mainScroll);
		mainScroll.setOnTouchListener(this);
		mainScroll.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollChanged(int x, int y, int oldX, int oldY) {
				android.util.Log.d("savvy", "x:" + oldX + "->" + x + ", y:"
						+ oldY + "->" + y);
				// if(y>oldY&&scrollFlag){//向后翻
				// if(currentPosition<2){
				// scrollFlag=false;
				// MainActivity.this.mainScroll
				// .scrollTo(
				// 0,
				// (int) positions[currentPosition +1]);// 改变滚动条的位置
				// currentPosition++;
				// }
				// }else{//向前翻
				// if(currentPosition>0&&scrollFlag){
				// scrollFlag=false;
				// MainActivity.this.mainScroll
				// .scrollTo(
				// 0,
				// (int) positions[currentPosition -1]);// 改变滚动条的位置
				// currentPosition--;
				// }
				// }
				if (y >= 0 && y <= height * 0.6) {
					tv_mail.setText("开始");
				} else if (y > height * 0.6 && y < height * 1.2) {
					tv_mail.setText("发现");
				} else {
					tv_mail.setText("记录");
				}
			}

			@Override
			public void onScrollStopped() {
				// scrollFlag=true;
				// if (mainScroll.isAtTop()) {
				// Toast.makeText(MainActivity.this, "Stopped at top",
				// Toast.LENGTH_SHORT).show();
				// } else if (mainScroll.isAtBottom()) {
				// Toast.makeText(MainActivity.this, "Stopped at bottom",
				// Toast.LENGTH_SHORT).show();
				// } else {
				// Toast.makeText(MainActivity.this, "Stopped",
				// Toast.LENGTH_SHORT).show();
				// }
			}

			@Override
			public void onScrolling() {
				android.util.Log.d("@", "scrolling...");
			}
		});
		// new Handler().postDelayed((new Runnable() {
		// @Override
		// public void run() {
		// mainScroll.scrollTo(0,(int) (height * 0.7 * 2));
		// }
		// }), 1);
		// mainScroll.setOnTouchListener(new OnTouchListener() {
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// switch (event.getAction()) {
		// case MotionEvent.ACTION_DOWN:
		// MainActivity.this.downY = event.getY();
		// break;
		// case MotionEvent.ACTION_UP:
		// float lastY = event.getY();
		// if (lastY > downY) {// 向后滑
		// if (currentPosition > 0) {
		// Runnable runnable = new Runnable() {
		// @Override
		// public void run() {
		// mainScroll
		// .scrollTo(
		// 0,
		// (int) positions[currentPosition - 1]);// 改变滚动条的位置
		// }
		// };
		// runnable.run();
		// }
		// } else {// 向前滑
		// if (currentPosition < 2) {
		// Runnable runnable = new Runnable() {
		// @Override
		// public void run() {
		// mainScroll
		// .scrollTo(
		// 0,
		// (int) positions[currentPosition + 1]);// 改变滚动条的位置
		// }
		// };
		// runnable.run();
		// }
		// }
		// }
		// return false;
		// }
		// });
	}

	private void initDragLayout() {
		dl = (DragLayout) findViewById(R.id.dl);
		dl.setDragListener(new DragListener() {
			@Override
			public void onOpen() {
				// lv.smoothScrollToPosition(new Random().nextInt(30));
				iv_icon.setChecked(true);

			}

			@Override
			public void onClose() {
				iv_icon.setChecked(false);
			}

			@Override
			public void onDrag(float percent) {

			}
		});
	}

	private void initView() {

		mainContentRl = (MyRelativeLayout) super
				.findViewById(R.id.mainContentRl);
		Bitmap bitmap0 = BitmapFactory.decodeResource(getResources(), R.drawable.tu0);
		Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.tu1);
		Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tu2);
		Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.tu3);
		Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.tu4);
		Bitmap bitmap5 = BitmapFactory.decodeResource(getResources(), R.drawable.tu5);
		image.add(bitmap0);
		image.add(bitmap1);
		image.add(bitmap2);
		image.add(bitmap3);
		image.add(bitmap4);
		image.add(bitmap5);
		tv_mail = (TextView) findViewById(R.id.tv_mail);
		listView_home = (ListView) findViewById(R.id.listview_home);

		hAdapter = new HomeListviewAdapter(MainActivity.this, image);
		listView_home.setAdapter(hAdapter);
		listView_home.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				if (position == 4) {
					Intent intent = new Intent(MainActivity.this, AngelActitivity.class);
					startActivity(intent);
				}else {
					
					Intent intent = new Intent(MainActivity.this, MainDetailActivity.class);
					startActivity(intent);
				}
			}
		});
		iv_icon = (RadioButton) findViewById(R.id.iv_icon);
		iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
		// tv_noimg = (TextView) findViewById(R.id.iv_noimg);

		adapter = new ImageAdapter(this);
		lv = (LinearLayout) findViewById(R.id.lv);
		// itemList=new ArrayList<String>();
		// for(int i=0;i<20;i++){
		// itemList.add("NO:"+i);
		// }
		addItemView();// 添加菜单item列表
		for (int i = 0; i < itemViewList.size(); i++) {
			lv.addView(itemViewList.get(i));
			itemViewList.get(i).getParent()
					.requestDisallowInterceptTouchEvent(true);// 屏蔽父控件拦截onTouch事件
		}

		iv_icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dl.open();// 打开菜单栏
			}

		});
	}

	public void addItemView() {
		itemViewList = new ArrayList<View>();
		// View view = getLayoutInflater().inflate(R.layout.pager_item, null);
		// ListView piList = (ListView) view.findViewById(R.id.piList);
		// List<String> itemList=new ArrayList<String>();
		// itemList.add("开始");
		// itemList.add("精选故事");
		// itemList.add("酷拉丁天使");
		// ItemListAdapter itemListAdapter=new ItemListAdapter(this, itemList);
		// piList.setAdapter(itemListAdapter);
		// itemViewList.add(view);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				(int) (width * 0.6), (int) (height * 0.7));
		RelativeLayout.LayoutParams params02 = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		params02.addRule(RelativeLayout.CENTER_IN_PARENT);
		RelativeLayout relativeLayout = new RelativeLayout(this);
		relativeLayout.setLayoutParams(params);
		ListView piList = new ListView(this);
		piList.setCacheColorHint(0);
		piList.setSelector(R.drawable.listviewselect);
		piList.setLayoutParams(params02);
		piList.setDividerHeight(0);
	 itemList = new ArrayList<Map<String, String>>();
		Map<String, String> map101 = new HashMap<String, String>();
		map101.put("text", "开始");
		map101.put("flag", "2");
		itemList.add(map101);
		Map<String, String> map102 = new HashMap<String, String>();
		map102.put("text", "精选故事");
		map102.put("flag", "1");
		itemList.add(map102);
		Map<String, String> map103 = new HashMap<String, String>();
		map103.put("text", "酷拉丁天使");
		map103.put("flag", "0");
		itemList.add(map103);
		ItemListAdapter itemListAdapter = new ItemListAdapter(this, itemList);
		piList.setAdapter(itemListAdapter);
		piList.setId(101);
		// piList.setOnTouchListener(this);
		piList.setOnItemClickListener(this);
		// piList.setOnTouchListener(this);
		relativeLayout.addView(piList);
		piList.getParent().requestDisallowInterceptTouchEvent(true);// 屏蔽父控件拦截onTouch事件
		// relativeLayout.setOnTouchListener(this);
		itemViewList.add(relativeLayout);

		RelativeLayout relativeLayout02 = new RelativeLayout(this);
		relativeLayout02.setLayoutParams(params);
		ListView piList2 = new ListView(this);
		piList2.setLayoutParams(params02);
		piList2.setSelector(R.drawable.listviewselect);
		piList.setCacheColorHint(0);
		piList2.setDividerHeight(0);
		 itemList2 = new ArrayList<Map<String, String>>();
		Map<String, String> map201 = new HashMap<String, String>();
		map201.put("text", "发现");
		map201.put("flag", "2");
		itemList2.add(map201);
		Map<String, String> map202 = new HashMap<String, String>();
		map202.put("text", "设计师");
		map202.put("flag", "1");
		itemList2.add(map202);
		Map<String, String> map203 = new HashMap<String, String>();
		map203.put("text", "施工队");
		map203.put("flag", "0");
		itemList2.add(map203);
		Map<String, String> map204 = new HashMap<String, String>();
		map204.put("text", "精品套餐");
		map204.put("flag", "0");
		itemList2.add(map204);
		Map<String, String> map205 = new HashMap<String, String>();
		map205.put("text", "我的需求");
		map205.put("flag", "0");
		itemList2.add(map205);
		ItemListAdapter itemListAdapter2 = new ItemListAdapter(this, itemList2);
		piList2.setAdapter(itemListAdapter2);
		piList2.setId(202);
		piList2.setOnItemClickListener(this);
		piList2.setCacheColorHint(0);
		// piList2.setOnTouchListener(this);
		relativeLayout02.addView(piList2);
		// relativeLayout02.setOnTouchListener(this);
		itemViewList.add(relativeLayout02);

		RelativeLayout relativeLayout03 = new RelativeLayout(this);
		relativeLayout03.setLayoutParams(params);
		ListView piList3 = new ListView(this);
		piList3.setLayoutParams(params02);
		piList3.setCacheColorHint(0);
		piList3.setSelector(R.drawable.listviewselect);
		piList3.setDividerHeight(0);
		 itemList3 = new ArrayList<Map<String, String>>();
		Map<String, String> map301 = new HashMap<String, String>();
		map301.put("text", "记录");
		map301.put("flag", "2");
		itemList3.add(map301);
		Map<String, String> map302 = new HashMap<String, String>();
		map302.put("text", "装修日记");
		map302.put("flag", "1");
		itemList3.add(map302);
		Map<String, String> map303 = new HashMap<String, String>();
		map303.put("text", "我的账单");
		map303.put("flag", "0");
		itemList3.add(map303);
		Map<String, String> map304 = new HashMap<String, String>();
		map304.put("text", "我的设计");
		map304.put("flag", "0");
		itemList3.add(map304);
		ItemListAdapter itemListAdapter3 = new ItemListAdapter(this, itemList3);
		piList3.setAdapter(itemListAdapter3);
		piList3.setId(303);
		piList3.setOnItemClickListener(this);
		// piList3.setOnTouchListener(this);
		relativeLayout03.addView(piList3);
		// relativeLayout03.setOnTouchListener(this);
		itemViewList.add(relativeLayout03);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			MainActivity.this.downY = event.getY();
			Log.e("savvy", MainActivity.this.downY + "按下");
			break;
		case MotionEvent.ACTION_UP:
			float lastY = event.getY();
			Log.e("savvy", lastY + "抬起");
			if (lastY - downY > 10) {// 向后滑
				Log.e("savvy", "向后滑");
				// if (currentPosition > 0) {
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						if (--currentPosition < 0) {
							currentPosition = 2;
						}
						mainScroll
								.scrollTo(0, (int) positions[currentPosition]);// 改变滚动条的位置
					}
				};
				runnable.run();
				// }else{
				// Log.e("savvy", "无效");
				// }
			} else if (downY - lastY > 10) {// 向前滑
				// if (currentPosition < 2) {
				if (++currentPosition > 2) {
					currentPosition = 0;
				}
				Log.e("savvy", "向前滑");
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						mainScroll
								.scrollTo(0, (int) positions[currentPosition]);// 改变滚动条的位置
					}
				};
				runnable.run();
				// }
			}
		}
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 != 0) {// 不为第一项
			switch(arg0.getId()){//判断是哪个listView被点击
			case 101:
				for(int i=1;i<itemList.size();i++){
					itemList.get(i).put("flag", "0");
				}
				Map<String, String> itemMap = itemList.get(arg2);
				itemMap.put("flag", "1");
				break;
			case 202:
				for(int i=1;i<itemList2.size();i++){
					itemList2.get(i).put("flag", "0");
				}
				Map<String, String> itemMap2 = itemList2.get(arg2);
				itemMap2.put("flag", "1");
				break;
			case 303:
				for(int i=1;i<itemList3.size();i++){
					itemList3.get(i).put("flag", "0");
				}
				Map<String, String> itemMap3 = itemList3.get(arg2);
				itemMap3.put("flag", "1");
				break;
			}
			((ItemListAdapter)arg0.getAdapter()).notifyDataSetChanged();
			AnimationSet set2 = new AnimationSet(true);
			TranslateAnimation tran2 = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.0f, // X轴开始位置
					Animation.RELATIVE_TO_SELF, 0.2f, // X轴移动的结束位置
					Animation.RELATIVE_TO_SELF, 0.0f, // Y轴开始位置
					Animation.RELATIVE_TO_SELF, 0.0f); // Y轴移动位置
			tran2.setDuration(500);// 设置动画持续时间
			set2.addAnimation(tran2); // 增加动画
			set2.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					AnimationSet set3 = new AnimationSet(true);
					TranslateAnimation tran3 = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0.2f, // X轴开始位置
							Animation.RELATIVE_TO_SELF, 0.0f, // X轴移动的结束位置
							Animation.RELATIVE_TO_SELF, 0.0f, // Y轴开始位置
							Animation.RELATIVE_TO_SELF, 0.0f); // Y轴移动位置
					tran3.setDuration(500);// 设置动画持续时间
					set3.addAnimation(tran3); // 增加动画
					mainContentRl.startAnimation(set3);
				}
			});
			mainContentRl.startAnimation(set2);
		}
	}

}
